package com.ruoyi.carUserService.util;/**
 * @author yang
 * @version 1.0
 * @data 2024/9/12 15:59
 */

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class SmsUtil {
    //生成验证码
    public static String createCode(){
        Random random = new Random();
        // 初始化一个StringBuilder来构建字符串
        StringBuilder sb = new StringBuilder();
        // 生成并拼接4位数字
        for (int i = 0; i < 4; i++) {
            // 生成一个0到9之间的随机数
            int digit = random.nextInt(10);
            // 将随机数转换为字符串并追加到StringBuilder中
            sb.append(digit);
        }
        // 将StringBuilder转换为String
        String randomSixDigitString = sb.toString();
        return randomSixDigitString;
    }

    //发送验证码操作
    public static void send(String phone,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI54OlGBtdVfPR","E7LeyXo2YZz6oh7cnw7YpnKVE9Yz4d");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "袁晓燕");
        request.putQueryParameter("TemplateCode", "SMS_206535389");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}


